package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.wx.CartBo;
import com.cskaoyan.bean.wx.CartCheckedOutVO;
import com.cskaoyan.bean.wxbean.*;
import com.cskaoyan.service.wx.CartService;
import com.cskaoyan.utils.IdUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/6 13:51
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    CartService cartService;

    @RequestMapping("index")
    public BaseRespVo cartIndex() {
        Integer id = IdUtils.getId();
        CartIndexVO cartIndexVO = cartService.getCartIndex(id);
        return BaseRespVo.ok(cartIndexVO);
    }

    @RequestMapping("update")
    public BaseRespVo cartUpdate(@RequestBody CartUpdateBO cartUpdateBO) {
        cartService.cartUpdate(cartUpdateBO.getId(), cartUpdateBO.getNumber());
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo cartDelete(@RequestBody CartDeleteBO cartDeleteBO) {
        List<Integer> productIds = cartDeleteBO.getProductIds();
        CartIndexVO cartIndexVO = new CartIndexVO();
        for (Integer productId : productIds) {
            if (productId != null) {
                cartIndexVO = cartService.cartDelete(productId);
            }
        }
        return BaseRespVo.ok(cartIndexVO);
    }

    /**
     * 选择或取消选择商品
     *
     * @return
     */
    @RequestMapping("checked")
    public BaseRespVo cartChecked(@RequestBody CartCheckedBo cartCheckedBo) {
        CartIndexVO cartIndexVO = cartService.cartChecked(cartCheckedBo, IdUtils.getId());
        return BaseRespVo.ok(cartIndexVO);
    }

    @RequestMapping("checkout")
    public BaseRespVo cartCheckOut(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId) {
        CartCheckedOutVO cartCheckedOutVO = cartService.CartCheckOut(cartId, addressId, couponId, grouponRulesId, IdUtils.getId());
        return BaseRespVo.ok(cartCheckedOutVO);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount() {
        if (IdUtils.getId() == 0) {
            return BaseRespVo.ok(0);
        }else {
            Integer goodsCount = cartService.getGoodsCount(IdUtils.getId());
            return BaseRespVo.ok(goodsCount);
        }
    }

    /**
     * 添加商品到购物车
     *
     * @return
     */
    @RequestMapping("add")
    public BaseRespVo CartAdd(@RequestBody CartBo cartBo) {
        if (IdUtils.getId() == 0) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setMsg("请登录");
            return baseRespVo;
        }
        long total = cartService.cartAdd(cartBo, IdUtils.getId());
        return BaseRespVo.ok(total);
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody CartBo cartBo) {
        if (IdUtils.getId() == 0) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(501);
            baseRespVo.setMsg("请登录");
            return baseRespVo;
        }
        Integer result = cartService.fastAdd(cartBo, IdUtils.getId());
        return BaseRespVo.ok(result);
    }



}
